const express = require('express')

const multer = require("multer") // 引入上传模块
const app = express()
const port = 3000

//跨域请求
app.all("*", function(req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "*");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method == 'OPTIONS') {
        res.sendStatus(200); //让options尝试请求快速结束
    } else {
        next();
    }
});
app.use(multer({ dest: "./public/upload/temp" }).any()) //指定上传文件的临时目录
app.use(express.json()) //json解析中间件
app.use(express.static('public'))




//注册TestController接口
app.use("/test", require("./controller/TestController"))

//注册stu接口
app.use("/stu", require("./controller/stuController"))

//注册study接口
app.use("/study", require("./controller/studyController"))

//注册zhsz接口
app.use("/zhsz", require("./controller/zhszController"))


//首页
app.get('/', (req, res) => {
    res.send('Hello World!')
})

//启动服务器
app.listen(port, () => {
    console.log(`启动成功localhost:${port}`)
})